import axios from 'axios';
import { publicPath } from '@/config';

axios.defaults.timeout = 180000;
axios.defaults.withCredentials = true;

// 请求拦截
axios.interceptors.request.use(
  (config) => {
    config.url = publicPath + config.url;
    console.log(config);
    return {
      ...config,
    };
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截
axios.interceptors.response.use(
  async (response) => {
    console.log('response', response);
    const { data } = response;

    return data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 统一处理接口请求异常
const request = async (param) => {
  let res = null;
  try {
    res = await axios(param);
  } catch (e) {
    console.log(e.message);
  }
  return res;
};

export default request;
